#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
from django.core.management.base import BaseCommand, CommandError
import tablib
from Backend.apps.Fund import *
from Tools import update_index
import time
import json


class Command(BaseCommand):
    help = 'create historical datas for the pre-added indices and funds'

    def add_arguments(self, parser):
        #parser.add_argument('poll_ids', nargs='+', type=int)
        pass

    def _add_index_update_script(self, script, description):
        cur = models.IndexUpdateScript.objects.filter(script = script)
        if cur.exists():
            #print(cur)
            return cur[0]
        else:
            return models.IndexUpdateScript.objects.create(script = script, description = description)

    def _add_index(self, number, name, description, link, update_script, script_des):
        cur = models.Index.objects.filter(number=number)
        if cur.exists():
            #print(cur)
            return cur[0]
        else:
            us = self._add_index_update_script(script = update_script, description = script_des)
            return models.Index.objects.create(number = number, name = name, description = description, link = link, update_script = us)
     
    def _add_fund_update_script(self, script, description):
        cur = models.IndexUpdateScript.objects.filter(script = script)
        if cur.exists():
            #print(cur)
            return cur[0]
        else:
            return models.IndexUpdateScript.objects.create(script = script, description = description)

    def _add_fund(self, index, number, name, description, link, update_script, script_des):
        cur = models.Fund.objects.filter(number=number)
        if cur.exists():
            #print(cur)
            return cur[0]
        else:
            us = self._add_fund_update_script(script = update_script, description = script_des)
            return models.Fund.objects.create(index = index, number = number, name = name, description = description, link = link, update_script = us)
     

    def calculate_increments(self, all_indices):
        for index in all_indices:
            self.stdout.write(self.style.SUCCESS('calculate_increments for index: "%s"' % index.number))
            try:
                all_states = models.IndexState.objects.filter(index = index).order_by('date')
            except Exception as err:
                self.stdout.write(self.style.ERROR('Can\'t find states for index: "%s":%s' % (index.number, str(err))))
            else:
                pre = None
                for st in  all_states:
                    if pre is None:
                        st.increment = 0
                        st.save()
                    else:
                        st.increment = st.market_value - pre.market_value
                        st.save()
                    pre = st

    def calculate_ave_30days(self, all_indices):
        for index in all_indices:
            self.stdout.write(self.style.SUCCESS('calculate_ave_30days for index: "%s"' % index.number))
            try:
                all_states = models.IndexState.objects.filter(index = index).order_by('date')
            except Exception as err:
                self.stdout.write(self.style.ERROR('Can\'t find states for index: "%s":%s' % (index.number, str(err))))
            else:
                pre30 = []
                for st in  all_states:
                    pre30.append(st.market_value)
                    if len(pre30) > 30:
                        pre30.pop(0)
                    st.ave_30day = sum(pre30) / len(pre30)
                    st.save()


    def handle(self, *args, **options):
        all_indices = []
        hushen300 = self._add_index("sh000300", "沪深300", "沪深300指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(hushen300)
        chuangyebanzhi = self._add_index("sz399006", "创业板指", "创业板指指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(chuangyebanzhi)
        keji100 = self._add_index("csi931187", "科技100", "科技100指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(keji100)
        shangzheng50 = self._add_index("sh000016", "上证50", "上证50指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(shangzheng50)
        shangzheng180 = self._add_index("sh000010", "上证180", "上证180指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(shangzheng180)
        shangzheng = self._add_index("sh000001", "上证指数", "上证指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(shangzheng)
        shengzhengchengzhi = self._add_index("sz399001", "深证成指", "深证成指指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(shengzhengchengzhi)
        xiaofei80 = self._add_index("sh000069", "消费80", "消费80指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(xiaofei80)
        zhongzheng500 = self._add_index("sh000905", "中证500", "中证500指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(zhongzheng500)
        zhengquangongsi = self._add_index("sz339375", "证券公司", "证券公司指数", "www.hushen300.com", "pass", "测试脚本")
        all_indices.append(zhengquangongsi)

        current_dir = os.path.dirname(os.path.abspath(__file__))
        index_data_dir = current_dir + "/IndexDatas"
        files = os.listdir(index_data_dir)
        #历史数据，不使用了
        if False:
            class ColumnIndex(object):
                DATE = 0
                NUMBER = 1
                NAME = 2
                PE_TTM = 3
                DANGER = 4
                MEDIUM = 5
                OPPOTUNITY = 6
                MARKET_VALUE = 7

            #从excel文件更新数据
            for f in files:
                if f.endswith(".xlsx"):
                    self.stdout.write(self.style.SUCCESS('begin to process file: "%s"' % f))
                    with open(index_data_dir + "/" + f, "rb") as df:
                        data_set = tablib.import_set(df.read(), format = 'xlsx')
                        if len(data_set) <= 0:
                            continue
                        try:
                            data = data_set[0]
                            if data[ColumnIndex.NUMBER] == 'cs931187':
                                data = list(data)
                                data[ColumnIndex.NUMBER] == 'csi931187'
                            index = models.Index.objects.get(number = data[ColumnIndex.NUMBER])
                        except models.Index.DoesNotExist:
                            self.stdout.write(self.style.ERROR('Can\'t find index for file: "%s"' % f))
                            continue
                        for data in data_set:
                            if data[ColumnIndex.NUMBER] == 'cs931187':
                                data = list(data)
                                data[ColumnIndex.NUMBER] == 'csi931187'
                            cur_state = models.IndexState.objects.filter(date = data[ColumnIndex.DATE], index = index)
                            if not cur_state.exists():
                                if data[ColumnIndex.MARKET_VALUE] is None:
                                    market_value = 0
                                else:
                                    market_value = data[ColumnIndex.MARKET_VALUE]
                                cur_state = models.IndexState.objects.create(index = index, date = data[ColumnIndex.DATE], pe = data[ColumnIndex.PE_TTM], pe_oppotunity = float(data[ColumnIndex.OPPOTUNITY]), pe_median = float(data[ColumnIndex.MEDIUM]), pe_danger = float(data[ColumnIndex.DANGER]), market_value = market_value)

                    self.stdout.write(self.style.SUCCESS('successfully processed file: "%s"' % f))

        #从网站实时下载数据
        for index in all_indices:
            self.stdout.write(self.style.SUCCESS('begin to update using http: "%s"' % index.name))
            index_datas = {}
            date = None
            update_index.update_index(index.number, index_datas, date)
            if not index.number in index_datas:
                self.stdout.write(self.style.ERROR('failed to updated using http: "%s"' % index.name))
                continue
            for data in index_datas[index.number]['values']:
                cur_state = models.IndexState.objects.filter(date = data['date'], index = index)
                if not cur_state.exists():
                    cur_state = models.IndexState.objects.create(index = index, date = data['date'], pe = data['pe'], pe_quantile = data['pe_quantile'], pe_oppotunity = data['pe_oppotunity'], pe_danger = data['pe_danger'], market_value = data['market_value'])
            date = time.strftime("%Y-%m-%d")
            with open(index_data_dir + "/" + index.number + "_" + date + ".json", "w") as df:
                df.write(json.dumps(index_datas))
            self.stdout.write(self.style.SUCCESS('successfully updated using http: "%s"' % index.name))

        #之前下载后保存的数据（json格式）
        for f in files:
            if f.endswith(".json"):
                self.stdout.write(self.style.SUCCESS('begin to process file: "%s"' % f))
                with open(index_data_dir + "/" + f, "r") as df:
                    data_dict = json.loads(df.read())
                    if data_dict is None or not type(data_dict) is dict:
                        continue
                    for k, v in data_dict.items():
                        try:
                            index = models.Index.objects.get(number = k)
                        except models.Index.DoesNotExist:
                            self.stdout.write(self.style.ERROR('Can\'t find index for %s in file: "%s"' % (k, f)))
                            continue
                        for data in v['values']:
                            cur_state = models.IndexState.objects.filter(date = data['date'], index = index)
                            if not cur_state.exists():
                                cur_state = models.IndexState.objects.create(index = index, date = data['date'], pe = data['pe'], pe_quantile = data['pe_quantile'], pe_oppotunity = data['pe_oppotunity'], pe_danger = data['pe_danger'], market_value = data['market_value'])
                self.stdout.write(self.style.SUCCESS('successfully processed file: "%s"' % f))
        
        #计算涨跌幅
        self.calculate_increments(all_indices)
        self.calculate_ave_30days(all_indices)

        self._add_fund()

                
